<template>
  <n-modal v-model:show="showModal" preset="card" style="width: 800px; max-width: 90vw">
    <div class="model-container">
      <div class="model-content">
        <div class="model-title-box">
          <div class="icon-title-legend"></div>
          <div class="title-text">器具箱详情</div>
          <div class="active-box">
            <n-button
              strong
              secondary
              round
              :size="'tiny'"
              type="primary"
              @click="showAccessRecord"
            >
              出入库记录
            </n-button>
            <n-switch
              :rail-style="railStyle"
              round
              :size="'small'"
              v-model:value="cabinetData.workingState"
              disabled
            >
              <template #checked>解锁</template>
              <template #unchecked>锁定</template>
            </n-switch>
          </div>
          <div class="close-box" @click="showModal = false"></div>
        </div>
        <div class="content-box">
          <div class="cabinet-title-box">
            <div class="cabinet-title">
              <div style="margin-right: 1rem">编码:</div>
              <div class="cabinet-code">{{ cabinetData.cabinetCode }}</div>
            </div>
            <div class="cabinet-num-box">
              <div class="cabinet-num-text">库存:</div>
              <div class="cabinet-num">{{ usedCount }}</div>
              <div class="cabinet-total">/{{ totalCount }}</div>
            </div>
          </div>
          <div class="cabinet-info-content">
            <div
              v-for="(floor, floorIndex) in Object.keys(cabinetInfo)"
              :key="floorIndex"
              class="cabinet-info-box"
            >
              <div
                v-for="(item, itemIndex) in cabinetInfo[floor]"
                :key="itemIndex"
                class="cabinet-info-item"
              >
                <div :title="item.storageCode" class="cabinet-cell-box">
                  <div v-if="item.whetherIn" class="battery-box">
                    <div class="charging-box"></div>
                    <div class="battery-num">{{ item.batteryNum }}%</div>
                  </div>
                  <div
                    v-if="item.whetherIn"
                    class="icon-shoe"
                    :style="{ '--color': item.color }"
                  ></div>
                </div>
                <div class="cabinet-info-num">{{ item.shoeCode }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </n-modal>
  <access-record-model ref="accessRecordModel" />
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue';
  import AccessRecordModel from './AccessRecordModel.vue';
  import { getCabinetAccountById } from '@/api/home/cabinetAccount';
  import { getCabinetStorageByCabinetId } from '@/api/home/cabinetStorage';

  // 定义响应式数据
  const showModal = ref(false);
  const cabinetData = ref<any>({});
  const cabinetInfo = ref<any>({});
  const usedCount = ref(0);
  const totalCount = ref(0);
  const accessRecordModel = ref();

  // 定义开关样式
  const railStyle = ({ focused, checked }) => {
    const style = {} as any;
    if (checked) {
      style.background = '#d03050';
    } else {
      style.background = '#2080f0';
    }
    return style;
  };

  // 显示模态框
  async function show(data: any) {
    showModal.value = true;
    cabinetData.value = data;
    await loadCabinetData();
    await loadCabinetStorage();
  }

  // 加载器具箱数据
  async function loadCabinetData() {
    try {
      const res = await getCabinetAccountById({ id: cabinetData.value.cabinetAccountId });
      cabinetData.value = res;
      cabinetData.value.workingState = cabinetData.value.workingState == 1 ? true : false;
    } catch (error) {
      console.error('加载器具箱数据失败:', error);
    }
  }

  // 加载器具箱存储信息
  async function loadCabinetStorage() {
    try {
      const res = await getCabinetStorageByCabinetId({ cabinetId: cabinetData.value.cabinetId });
      totalCount.value = res.length;
      cabinetInfo.value = {};
      usedCount.value = 0;

      res.forEach((item: any) => {
        if (!cabinetInfo.value[item.floor]) {
          cabinetInfo.value[item.floor] = [];
        }

        item.batteryNum = 0;
        item.color = '#FFB800';

        if (item.batteryLevel) {
          item.batteryNum = parseInt(item.batteryLevel.replace('%', ''));
          if (item.batteryNum === 100) {
            item.color = '#00FF19';
          }
        }

        if (item.whetherIn) {
          usedCount.value++;
        }

        cabinetInfo.value[item.floor].push(item);
      });
    } catch (error) {
      console.error('加载器具箱存储信息失败:', error);
    }
  }

  // 显示出入库记录
  function showAccessRecord() {
    accessRecordModel.value?.show(cabinetData.value);
  }

  // 暴露方法给父组件
  defineExpose({
    show,
  });
</script>

<style scoped>
  .model-container {
    width: 100%;
  }

  .model-content {
    width: 100%;
  }

  .model-title-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 2.5rem;
    border-bottom: 1px solid #e5e7eb;
    margin-bottom: 1rem;
  }

  .icon-title-legend {
    width: 0.25rem;
    height: 1.25rem;
    background: #3b82f6;
    border-radius: 1px;
  }

  .title-text {
    font-size: 1.125rem;
    font-weight: 500;
    color: #1f2937;
    flex: 1;
    margin-left: 0.5rem;
  }

  .active-box {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .close-box {
    width: 1.5rem;
    height: 1.5rem;
    cursor: pointer;
  }

  .content-box {
    width: 100%;
  }

  .cabinet-title-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid #e5e7eb;
  }

  .cabinet-title {
    display: flex;
    align-items: center;
    font-size: 1rem;
    font-weight: 500;
  }

  .cabinet-code {
    font-weight: bold;
    color: #1f2937;
  }

  .cabinet-num-box {
    display: flex;
    align-items: baseline;
  }

  .cabinet-num-text {
    font-size: 0.875rem;
    color: #6b7280;
  }

  .cabinet-num {
    font-size: 1.125rem;
    font-weight: bold;
    color: #1f2937;
  }

  .cabinet-total {
    font-size: 0.875rem;
    color: #6b7280;
  }

  .cabinet-info-content {
    display: flex;
    flex-wrap: wrap;
    margin-top: 1rem;
  }

  .cabinet-info-box {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    margin-bottom: 1rem;
  }

  .cabinet-info-item {
    width: 25%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 1rem;
  }

  .cabinet-cell-box {
    width: 4rem;
    height: 4rem;
    border: 1px solid #d1d5db;
    border-radius: 0.25rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
  }

  .battery-box {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .charging-box {
    width: 1.5rem;
    height: 2rem;
    border: 1px solid #9ca3af;
    border-radius: 2px;
    position: relative;
    margin-bottom: 0.25rem;
  }

  .charging-box::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 70%;
    background-color: #10b981;
  }

  .battery-num {
    font-size: 0.75rem;
    color: #6b7280;
  }

  .icon-shoe {
    width: 2rem;
    height: 1rem;
    background-color: var(--color);
    clip-path: polygon(0 0, 100% 0, 80% 100%, 20% 100%);
    margin-top: 0.25rem;
  }

  .cabinet-info-num {
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: #374151;
  }
</style>
